package com.example.bai9;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

//	enum Operator {sum,subtract,multi,divide}
	
	EditText edta;
	EditText edtb;
	
	Button btnsum;
	Button btnsubtract;
	Button btnmulti;
	Button btndivide;
	
	TextView txtresult;
	ListView listhistory;
	
	ArrayList<String> arr;
	ArrayAdapter<String> adapter;
	
	OnClickListener listener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loadTabs();
        getControls();
    }
    private void operateCal(int id)
    {
    	int a=Integer.valueOf(edta.getText().toString());
    	int b=Integer.valueOf(edtb.getText().toString());
    	int res=0;
    	switch (id) {
		case R.id.btnsum:
			{
			res=a+b;
			txtresult.setText(a+"+"+b+"="+res);
			break;
			}
			case R.id.btnsubtract:
			{
			res=a-b;
			txtresult.setText(a+"-"+b+"="+res);
			break;
			}
			case R.id.btnmulti:
			{
			res=a*b;
			txtresult.setText(a+"*"+b+"="+res);
			break;
			}
			case R.id.btndivide:
			{
				if(b>0)
				{
					res=a/b;
					txtresult.setText(a+"/"+b+"="+res);
				}
				else
				{
					txtresult.setText("b must <> 0");
				}
			break;
			}
		default:
			break;
		}
    	
    	arr.add(txtresult.getText().toString());
    	adapter.notifyDataSetChanged();
    }
    private void getControls()
    {
    	 edta= (EditText) findViewById(R.id.edta);
    	 edtb= (EditText) findViewById(R.id.edtb);
    	
    	 btnsum= (Button) findViewById(R.id.btnsum);
    	 btnsubtract= (Button) findViewById(R.id.btnsubtract);
    	 btnmulti= (Button) findViewById(R.id.btnmulti);
    	 btndivide= (Button) findViewById(R.id.btndivide);
    	
    	 txtresult = (TextView) findViewById(R.id.txtresult);
    	 listhistory= (ListView) findViewById(R.id.listhistory);
    	
    	arr= new ArrayList<String>();
    	adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr);
    	listhistory.setAdapter(adapter);
    	
    	listener= new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				operateCal(v.getId());
			}
		};
		btnsum.setOnClickListener(listener);
		btnsubtract.setOnClickListener(listener);
		btnmulti.setOnClickListener(listener);
		btndivide.setOnClickListener(listener);
    }
    
    private void loadTabs()
    {
    	final TabHost host= (TabHost) findViewById(R.id.host);
    	host.setup();
    	TabHost.TabSpec spec;
    	spec = host.newTabSpec("t1");
    	spec.setContent(R.id.tab1);
    	spec.setIndicator("1-CALCULATOR");
    	host.addTab(spec);
    	
    	spec= host.newTabSpec("t2");
    	spec.setContent(R.id.tab2);
    	spec.setIndicator("2-HISTORY");
    	host.addTab(spec);
    	
    	host.setCurrentTab(0);
    	
    	host.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				String s="Tag= "+tabId+"- Index="+host.getCurrentTab();
				Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
